package com.byt;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
@EnableScheduling
public class Task {

    private Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 消费cpu
     */
    @Scheduled(cron = "*/10 * * * * ?")
    public void cpu() {
        try {
            double cpu = CpuServer.cpuInfo();
            logger.info("cpu占有率："+cpu);
            if (cpu < 60) {
                AsyncExecutor.exec(() -> {
                    CpuServer.cpuConsume();
                });
            }
        } catch (Exception e) {
            logger.error("消费cpu[" + DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now())
                    + "]com.byt.cpu()", e);
        }
    }

    /**
     * 消费内存
     */
    @Scheduled(cron = "*/10 * * * * ?")
    public void RAM() {
        try {
            AsyncExecutor.exec(() -> {
                RAMServer1.RAM();
            });
        } catch (Exception e) {

        }
    }
}
